<?php

class Skjb_Directory
{
    public $path = null;
    
    public $name = null;
    
    protected $_pointer = null;
    
    protected $_contents = null;
    
    protected $_files = null;
    
    protected $_directories = null;
    
    protected $_ignore = array();
    
    public function __construct($path)
    {
        $this->path = realpath($path);
        $this->name = basename($this->path);
    }
    
    public function __get($name)
    {
        switch ($name) {
            case 'contents':
                if ($this->_contents == null) {
                    $this->_contents = new Skjb_Directory_Iterator($this, Skjb_Directory_Iterator::FILTER_ALL, $this->_ignore);
                }
                return $this->_contents;
            case 'files':
                if ($this->_files == null) {
                    $this->_files = new Skjb_Directory_Iterator($this, Skjb_Directory_Iterator::FILTER_FILES, $this->_ignore);
                }
                return $this->_files;
        	case 'directories':
                if ($this->_directories == null) {
                    $this->_directories = new Skjb_Directory_Iterator($this, Skjb_Directory_Iterator::FILTER_DIRECTORIES, $this->_ignore);
                }
                return $this->_directories;
        }
        throw new Zend_Exception('Unknown property "' . $name . '"');
    }
    
    public function isDirectory()
    {
        return true;
    }
    
    public function isFile()
    {
        return false;
    }
    
    public function ignore($ignore)
    {
        if (!is_array($ignore)) {
            $ignore = array($ignore);
        }
        $this->_ignore = $ignore;
        
        return $this;
    }
    
    public function getDirectory($name)
    {
        foreach ($this->directories as $directory) {
            if ($directory->name == $name) {
                return $directory;
            }
        }
        return false;
    }
    
    public function deleteFiles()
    {
        foreach ($this->files as $file) {
            $file->delete();
        }
        $this->_files = null;
    }
    
    public function deleteContents($recursive = false)
    {
        foreach ($this->contents as $content) {
            $content->delete($recursive);
        }
        $this->_contents = null;
    }
    
    public function delete($recursive = false)
    {
        if (!file_exists($this->path)) {
            return false;
        }
        if ($recursive) {
            /*if (DIRECTORY_SEPARATOR == '/') {
                shell_exec('rm -rf ' . $this->path);
            } else {
                shell_exec('rd /s /q ' . $this->path);
            }*/
            $this->deleteContents(true);
            //clearstatcache();
        }
        $delete = @rmdir($this->path);
        if ($delete === false) {
            //throw new Zend_Exception('Unable to delete "' . $this->path . '"');
        }
    }
    
    public static function load($path)
    {
        return new Skjb_Directory($path);
    }
}